package com.zhaiyz.grpcdemo

import com.github.kristofa.brave.Brave
import com.github.kristofa.brave.EmptySpanCollectorMetricsHandler
import com.github.kristofa.brave.Sampler
import com.github.kristofa.brave.grpc.BraveGrpcClientInterceptor
import com.github.kristofa.brave.http.HttpSpanCollector
import com.zhaiyz.grpcdemo.proto.GreetingRequest
import com.zhaiyz.grpcdemo.proto.GreetingServiceGrpc
import io.grpc.ManagedChannelBuilder
import java.util.concurrent.TimeUnit
import kotlin.system.exitProcess


/**
 * Created by zhaiyz on 2018/1/17.
 */
fun main(args: Array<String>) {
    val channel = ManagedChannelBuilder
            .forAddress("localhost", 8080)
            .usePlaintext(true)
            .intercept(BraveGrpcClientInterceptor(brave("greetingClient")))
            .build()
    val blockStub = GreetingServiceGrpc.newBlockingStub(channel)
    val futureStub = GreetingServiceGrpc.newFutureStub(channel)
    val stub = GreetingServiceGrpc.newStub(channel)

    val request = GreetingRequest.newBuilder().setName("zhaiyz").build()


    block(blockStub, request)

//    future(futureStub, request)
//
//    async(stub, request)
//
//    streamRequest(stub)
//
//    streamResponse(stub, request)
//
//    streamOfStream(stub)
//
//    channel.awaitTermination(10, TimeUnit.SECONDS)

    exitProcess(0)
}

fun brave(serviceName: String): Brave {
    return Brave.Builder(serviceName)
            .traceSampler(Sampler.ALWAYS_SAMPLE)
            .spanCollector(HttpSpanCollector.create(String.format("http://%s:9411", "localhost"),
                    EmptySpanCollectorMetricsHandler()))
            .build()
}